import { UserDto } from "../../api/user"
import {getCurrentReservation, ReservationDto} from "../../api/reservation"
import { LENGYU_SUCCESS_CODE } from "../../utils/request"

// pages/accessControl/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnDisabled:true,
    user:{} as UserDto,
    reservation:{} as ReservationDto
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init(),
    this.checkPermission()
  },
  async checkPermission(){
    if(this.data.user.isTjgk){
      this.setData({
        "btnDisabled":false
      })
    }
    else{
      //调用接口确认
      let res = await getCurrentReservation()
      console.log("获取正在拜访的信息",res)
      if(res.code == LENGYU_SUCCESS_CODE){
        this.setData({
          reservation:res.data as ReservationDto
        })
        if(this.data.reservation != null && (this.data.reservation.id != undefined && this.data.reservation.id.length > 0)){
          this.setData({
            btnDisabled:false
          })
        }
      }
      else{
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000
        })
      }
    }
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  openDoor(e:any){
    console.log("点击开闸按钮",e.currentTarget.dataset.field)
    // wx.showToast({
    //   title: "开闸成功",
    //   icon: 'success',
    //   duration: 2000
    // })
    wx.showToast({
      title: "开闸失败",
      icon: 'error',
      duration: 2000
    })
  },
  init(){
    let userStr =  wx.getStorageSync("user");
    let user1:UserDto = JSON.parse(userStr) as UserDto
    this.setData({
      "user":user1
    })
  },
})